package com.shisfish.core.Test;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mail{
	
	private MimeMessage mimeMsg;
	private Session session;
	private Properties properties;
	private String username;
	private String password;
	private Multipart multipart;
	
	public Mail(String smtp) {
		
		setSmtpHost(smtp);
		
		createMimeMessage();
		
	}
	
	public void setSmtpHost(String hostName) {
		
		System.out.println("mail.smtp.host = " + hostName);
		
		if (properties == null) {
			properties = System.getProperties();
			System.out.println(properties);
		}
		
		properties.put("mail.smtp.host", hostName);
		
	}
	
	public boolean createMimeMessage() {
		try {
			System.out.println("准备获取邮件会话对象！");
			session = Session.getDefaultInstance(properties, null);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("获取邮件会话错误！" + e);
			return false;
		}
		
		try {
			 System.out.println("准备创建MIME邮件对象！");
			 mimeMsg = new MimeMessage(session);
			 multipart = new MimeMultipart();
			 
			 return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("创建MIME邮件对象失败！" + e);
			
            return false;
		}

	}
	
	/*定义SMTP是否需要验证*/
	public void setNeedAuth(boolean need) {
		System.out.println("mail.smtp.auth = " + need);
		
		if (properties == null) {
			properties = System.getProperties();
			System.out.println(properties);
		}
		
		if (need) {
			properties.put("mail.smtp.auth", "true");
		}
		else {
			properties.put("mail.smtp.auth", "false");
		}
	}

	public void setNamePass(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	/*定义邮件主题*/
	public boolean setSubject(String mailSubject) {
		System.out.println("定义邮件主题！");
		try {
			mimeMsg.setSubject(mailSubject);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println("定义邮件主题发生错误！");
			return false;
		}
		
	}
	
	/*定义邮件正文*/
	public boolean setBody(String mailBody) {
		try {
			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setContent("" + mailBody, "text/html; charset=GBK");
			multipart.addBodyPart(bodyPart);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("定义邮件正文时发生错误！" + e);
            return false;
		}
	}
	
	/*设置发信人*/
	public boolean setFrom(String from) {
		try {
			System.out.println("设置发信人！");
            mimeMsg.setFrom(new InternetAddress(from)); //发信人
            return true;
        } catch (Exception e) {
        	// TODO: handle exception
            return false;
        }
	}
	 
	/*定义收信人*/
	public boolean setTo(String to) {
        if (to == null) {
            return false;
        }
        
        try {
        	System.out.println("定义收信人！");
            mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            return true;
        } catch (Exception e) {
        	// TODO: handle exception
            return false;

        }

    }
	
	/*定义抄送人*/
    public boolean setCopyTo(String copyto) {
        if (copyto == null) {
            return false;
        }
        try {
            mimeMsg.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress
                    .parse(copyto));
            return true;
        } catch (Exception e) {
        	// TODO: handle exception
            return false;
        }
    }
	 
    /*发送邮件模块*/
    public boolean sendOut() {
        try {
            mimeMsg.setContent(multipart);
            mimeMsg.saveChanges();
            System.out.println("邮件发送中....");
            Session mailSession = Session.getInstance(properties, null);
            Transport transport = mailSession.getTransport("smtp");
            transport.connect((String) properties.get("mail.smtp.host"), username, password);
            transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
            System.out.println("发送成功！");
            transport.close();
            return true;
        } catch (Exception e) {
        	// TODO: handle exception
            System.err.println("邮件失败！" + e);
            return false;
        }

    }    
	        
    public static boolean sendAndCc(String smtp, String from, String to, String copyto,
        String subject, String content, String username, String password) {
        Mail theMail = new Mail(smtp);
        theMail.setNeedAuth(true); // 验证
        if (!theMail.setSubject(subject))
            return false;
        if (!theMail.setBody(content))
            return false;
        if (!theMail.setTo(to))
            return false;
        if (!theMail.setCopyTo(copyto))
            return false;
        if (!theMail.setFrom(from))
            return false;
        theMail.setNamePass(username, password);
        if (!theMail.sendOut())
            return false;
        return true;
    }    

    public static void main(String[] args) {
        String smtp = "smtp.163.com";// smtp服务器
        String from = "shuangsjye@163.com";// 邮件显示名称
        String to = "sophia.1503@foxmail.com";// 收件人的邮件地址，必须是真实地址
        String copyto = "";// 抄送人邮件地址
        String subject = "测试邮件";// 邮件标题
        String content = "你好！";// 邮件内容
        String username = "11111111";// 发件人真实的账户名
        String password = "shjjjysj19940112";// 发件人密码
        sendAndCc(smtp, from, to, copyto, subject, content, username, password);
    }
	    
}

